﻿using ALTP.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace ALTP.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult test()
        {

            return View();
        }
         
        public ActionResult QuestionJson(int? level)
        {
            using (ALTPEntities db = new ALTPEntities())
            {
                var lst = db.Quesion.Where(e=>e.Level==level).ToList();
                int count = lst.Count();
                int index = new Random().Next(count);
                Quesion obj = lst.ElementAt(index);
                return Json(new { Id = obj.Id, Question = obj.Question, Choice1 = obj.Choice1, Choice2 = obj.Choice2, Choice3 = obj.Choice3, Choice4 = obj.Choice4, Answer = obj.Answer, Level=obj.Level }, JsonRequestBehavior.AllowGet);
            }
          
        }
        public ActionResult AnswerJson(int? Id, int? Answer)
        {
            using (ALTPEntities db = new ALTPEntities())
            {
                var obj = db.Quesion.Where(e => e.Id == Id).FirstOrDefault();
                bool IsTrue=false;
                string vnd="0";
                if (obj.Answer == Answer) {
                    IsTrue = true;
                    vnd=VND((int)obj.Level);
                } 
                return Json(new { Answer = obj.Answer, Level = obj.Level, IsTrue = IsTrue, vnd = vnd });
            } 
        }
        public string VND(int level) {
            if (level == 1)
            {
                return "200.000"; ;
            }
            else if (level == 2)
            {
                return "400.000";
            }
            else if (level == 3)
            {
                return "500.000";
            }
            else if (level == 4)
            {
                return "1.000.000";
            }
            else if (level == 5)
            {
                return "200.000";
            }
            else if (level == 6)
            {
                return "300.000";
            }
            else if (level == 7)
            {
                return "600.000";
            }
            else if (level == 8)
            {
                return "10.000.000";
            }
            else if (level == 9)
            {
                return "14.000.000";
            }
            else if (level == 10)
            {
                return "22.000.000";
            }
            else if (level == 11)
            {
                return "30.000.000";
            }
            else if (level == 12)
            {
                return "40.000.000";
            }
            else if (level == 13)
            {
                return "60.000.000";
            }
            else if (level == 14)
            {
                return "85.000.000";
            }
            else if (level == 15)
            {
                return "15.000.000";
            }
            return "0";
        }
        public ActionResult TroGiup50Json(int? Id )
        { 
            using (ALTPEntities db = new ALTPEntities())
            {
                var obj = db.Quesion.Where(e => e.Id == Id).FirstOrDefault(); 
                List<int> lst = new List<int> { 1, 2, 3, 4 };
                lst.Remove((int)obj.Answer);
                var  index = new Random().Next(0,2);
                lst.RemoveAt(index);

                return Json(new { sai1 = lst.ElementAt(0), sai2 = lst.ElementAt(1) });
            }  
        }
        public ActionResult khangiaJson(int? Id)
        {
            using (ALTPEntities db = new ALTPEntities())
            {
                var obj = db.Quesion.Where(e => e.Id == Id).FirstOrDefault();
                int total = 100;
                 
                int Choice1=new Random().Next(75);
                int Choice2= new Random().Next(total-Choice1);
                int Choice3 = new Random().Next(total - (Choice1 + Choice2));
                int Choice4 = new Random().Next(total - (Choice1 + Choice2+ Choice3));
                return Json(new { Choice1 = Choice1, Choice2 = Choice2, Choice3 = Choice3, Choice4 = Choice4 }); 
            }
        }
        public ActionResult chuyengiaJson(int? Id,int? cgId)
        {
            using (ALTPEntities db = new ALTPEntities())
            {
                var obj = db.Quesion.Where(e => e.Id == Id).FirstOrDefault();
                List<int> lst = new List<int> { 1, 2, 3, 4 };
                lst.Add((int)obj.Answer);
                lst.Add((int)obj.Answer);
                lst.Add((int)obj.Answer);
                lst.Add((int)obj.Answer);
                lst.Add((int)obj.Answer);
                var index = new Random().Next(0,lst.Count);
                var Answer = lst.ElementAt(index);
                var Choice = "";
                if (Answer == 1) {
                    Choice = obj.Choice1;
                }
                else if (Answer == 2) {
                    Choice = obj.Choice2;
                }
                else if (Answer == 3)
                {
                    Choice = obj.Choice3;
                }
                else if (Answer == 4)
                {
                    Choice = obj.Choice4;
                }
                return Json(new { Id = obj.Id, Answer = Answer, Choice = Choice });
            }
        }
        public ActionResult Index()
        { 
          
            return View();
        }




        public ActionResult PopupThongbao()
        {
            return PartialView();
        }



        public ActionResult play()
        {
            return PartialView();
        }
        public ActionResult ListQuestion()
        {
            return PartialView();
        }
        public ActionResult QuestionView()
        {
            return PartialView();
        }
        public ActionResult ketqua()
        {
            return PartialView();
        }


        public ActionResult Thongbao()
        {
            return PartialView();
        }
        public ActionResult khangia()
        {
            return PartialView();
        }
        public ActionResult chuyengialst()
        {
            return PartialView();
        }
        public ActionResult chuyengia()
        {
            return PartialView();
        }
   
        public PartialViewResult Nav()
        {
            return PartialView();
        }

    }
}
